{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.26730978 0.10170209]]\n",
      "[[0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "# 创建两个变量\n",
    "tf.reset_default_graph()  \n",
    "v1= tf.Variable(tf.random_normal([1, 2], stddev=0.35), name=\"v1\")\n",
    "v2= tf.Variable(tf.zeros([1,2]), name=\"v2\")\n",
    "\n",
    "# 添加用于初始化变量的节点\n",
    "init_op = tf.global_variables_initializer()\n",
    "\n",
    "# Create a saver.\n",
    "saver = tf.train.Saver(tf.global_variables())\n",
    "\n",
    "# 运行图，打开会话，每1000次保存一个模型\n",
    "sess = tf.Session()\n",
    "step = 1000\n",
    "sess.run(init_op)\n",
    "saver.save(sess, './save/my-model', global_step=step)\n",
    "print(sess.run('v1:0'))\n",
    "print(sess.run('v2:0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "latest_model: ./save\\my-model-1000\n",
      "INFO:tensorflow:Restoring parameters from ./save\\my-model-1000\n",
      "[[0.26730978 0.10170209]]\n",
      "[[0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "with tf.Session() as sess:    \n",
    "    saver = tf.train.import_meta_graph('./save/my-model-1000.meta')\n",
    "    module_file = tf.train.latest_checkpoint('./save')\n",
    "    print('latest_model:', module_file)\n",
    "    saver.restore(sess, module_file)\n",
    "#     saver.restore(sess,tf.train.latest_checkpoint('./save'))\n",
    "    print(sess.run('v1:0'))\n",
    "    print(sess.run('v2:0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "latest_model: ./save\\my-model-1000\n",
      "INFO:tensorflow:Restoring parameters from ./save\\my-model-1000\n",
      "[[0.26730978 0.10170209]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "with tf.Session() as sess:    \n",
    "    saver = tf.train.import_meta_graph('./save/my-model-1000.meta')\n",
    "    module_file = tf.train.latest_checkpoint('./save')\n",
    "    print('latest_model:', module_file)\n",
    "    saver.restore(sess, module_file)\n",
    "#     saver.restore(sess,tf.train.latest_checkpoint('./save'))\n",
    "    print(sess.run('v1:0'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
